﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;

namespace DNA.Mvc
{
    /// <summary>
    /// Defines the model class that describe a package.
    /// </summary>
    /// <remarks>
    /// Package is use to describe a set of files.
    /// </remarks>
    public class Package
    {
        public Package() 
        {
            Authors = new List<Contact>();
            Licenses = new List<Res>();
            Links = new List<Res>();
            Images = new List<ImageRes>();
            Published = DateTime.Now;
        }

        /// <summary>
        /// Gets/Sets the uri or virtual path of the package manifest.xml file.
        /// </summary>
        public virtual string Url { get; set; }

        /// <summary>
        /// Gets/Sets the package name.
        /// </summary>
        [Required]
        public virtual string Name { get; set; }

        /// <summary>
        /// Gets/Sets the display text of the package.
        /// </summary>
        public virtual string Title { get; set; }

        /// <summary>
        /// Gets/Sets the package description.
        /// </summary>
        public virtual string Description { get; set; }

        /// <summary>
        /// Gets/Sets the language locale of the package.
        /// </summary>
        public virtual string Language { get; set; }

        /// <summary>
        /// Gets/Sets the version of the package.
        /// </summary>
        public virtual string Version { get; set; }

        /// <summary>
        /// Gets/Sets the release notes of the package.
        /// </summary>
        public virtual string ReleaseNotes { get; set; }

        /// <summary>
        /// Gets/Sets the package publish date.
        /// </summary>
        public DateTime Published { get; set; }

        /// <summary>
        /// Gets/Sets the authors of the package.
        /// </summary>
        public virtual ICollection<Contact> Authors { get; set; }

        /// <summary>
        /// Gets/Sets the licenses of the package.
        /// </summary>
        public virtual ICollection<Res> Licenses { get; set; }

        /// <summary>
        /// Gets/Sets the links of the package.
        /// </summary>
        public virtual ICollection<Res> Links { get; set; }

        /// <summary>
        /// Gets / Sets images of the package.
        /// </summary>
        public virtual ICollection<ImageRes> Images { get; set; }

        /// <summary>
        /// Gets the default image object, it's the first image of Images property
        /// </summary>
        public virtual ImageRes DefaultImage
        {
            get
            {
                if (Images != null)
                {
                    if (Images.Count(i => i.IsDefault) > 0)
                        return Images.First(i => i.IsDefault);
                    else
                        return Images.FirstOrDefault();
                }
                return null;
            }
        }

        /// <summary>
        /// Gets the default author, it's the first author of Authors property.
        /// </summary>
        public virtual Contact DefaultAuthor
        {
            get
            {
                if (Authors != null)
                    return Authors.FirstOrDefault();
                return null;
            }
        }

    }
}
